using System.Collections.Generic;
using System.Web.Mvc;
using RecruitmentAgency.Models.DataTypes;
using RecruitmentAgency.Models.Utilities;
using RecruitmentAgency.Models.Validation;

namespace RecruitmentAgency.Controllers
{
    /// <summary>
    /// The RecruitmentAgency.Controllers.JobProviderController provides interaction for the job provider pages on the RecruitmentAgency web site
    /// </summary>
    public class JobProviderController : BaseController
    {
        private readonly List<string> sectors, titles;
        private readonly JobProviderValidator validator;
        private const string SelectMessage = "Please select an option from the list";
        /// <summary>
        /// Initialises a new RecruitmentAgency.Controllers.JobProviderController instance
        /// </summary>
        public JobProviderController()
        {
            titles = CommonMethods.AddSelectMessageToList(DataProvider.GetTitles());
            sectors = CommonMethods.AddSelectMessageToList(DataProvider.GetSectors());
            validator = new JobProviderValidator(ModelState, SelectMessage);
        }

        /// <summary>
        /// This GET ViewCompanies method returns a JobProviderSearchData instance that contains one page of all job provider results
        /// </summary>
        /// <param name="page">The page number of results required</param>
        /// <returns>A list of job provider instances</returns>
        public ActionResult ViewCompanies(int? page)
        {
            if (Request.QueryString["pageSize"] != null) PageSize = int.Parse(Request.QueryString["pageSize"]);
            ViewData["pageSizes"] = new SelectList(DefaultPageSizes, PageSize);
            int pageNumber = page.HasValue ? page.Value : 1;
            return View(JobProviderDataProvider.SearchJobProviders(PageSize, pageNumber, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty));
        }

        /// <summary>
        /// This POST ViewCompanies method returns a JobProviderSearchData instance dependant on the search query
        /// </summary>
        /// <param name="page">The page number of results required</param>
        /// <param name="searchData">The JobProviderSearchData instance that holds the search parameters from the form</param>
        /// <returns>A list of job provider instances dependant on the search query</returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ViewCompanies(int? page, JobProviderSearchData searchData)
        {
            int pageNumber = page.HasValue ? page.Value : 1;
            return View(JobProviderDataProvider.SearchJobProviders(PageSize, pageNumber, searchData.FirstName, searchData.LastName, searchData.CompanyName, searchData.RegistrationNumber, searchData.City));
        }

        /// <summary>
        /// This GET ViewCompany method returns a JobProvider instance containing all of the details related to the input ID parameter
        /// </summary>
        /// <param name="id">The ID number of the job provider whose details are required</param>
        /// <returns>A JobProvider instance</returns>
        public ActionResult ViewCompany(int id)
        {
            // sorry, but it was only one line.
            return View(JobProviderDataProvider.GetJobProvider(id));
        }

        /// <summary>
        /// This GET Edit method returns a JobProvider instance containing all of the details related to the input ID parameter
        /// </summary>
        /// <param name="id">The ID number of the job provider whose details are required</param>
        /// <returns>A JobProvider instance</returns>
        public ActionResult Edit(int id)
        {
            JobProvider jobProvider = JobProviderDataProvider.GetJobProvider(id);
            FillViewData(jobProvider);
            return View(jobProvider);
        }

        
        /// <summary>
        /// This Post Edit method updates any input values passed from the View via the Model and returns a CvData instance containing all of the CV details related to the input ID parameter
        /// </summary>
        /// <param name="id">The id of the instance to update</param>
        /// <param name="jobProvider">The jobProviderinstance that is bound to the View</param>
        /// <returns>A JobProvider instance</returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id,JobProvider jobProvider)
        {
            if (validator.ValidateJobProvider(jobProvider))
            {
                jobProvider.JobProviderId = id;
                JobProviderDataProvider.UpdateJobProvider(jobProvider);                
            }
            FillViewData(jobProvider);
            return View(jobProvider);
        }

        private void FillViewData(JobProvider jobProvider)
        {
            ViewData["titles"] = new SelectList(titles, jobProvider.Title);
            ViewData["sectors"] = new SelectList(sectors, jobProvider.Sector);
        }

        /// <summary>
        /// This GET Delete method returns a JobProvider instance relating to the input ID parameter to be deleted
        /// </summary>
        /// <param name="id">The ID number of the job provider to be deleted</param>
        /// <returns>A JobProvider instance</returns>
        public ActionResult Delete(int id)
        {
            return View(JobProviderDataProvider.GetJobProvider(id));
        }

        /// <summary>
        /// This POST Delete method deletes the input JobProvider and then redirects to the GET ViewCompanies method which returns a List of JobProvider instances
        /// </summary>
        /// <param name="id">The ID number of the user to be deleted</param>
        /// <param name="jobProvider">The JobProvider instance to be deleted</param>
        /// <returns>A redirect to the ViewCompanies page</returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id, JobProvider jobProvider)
        {
            JobProviderDataProvider.DeleteJobProvider(id);
            return RedirectToAction("ViewCompanies");
        }
    }
}